package com.demo.interceptor;

import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

@Component
public class MybatisConfigInterceptor implements InnerInterceptor {

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        // 后续可以通过语句拼接切换数据库
//        PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
//        TableNameParser tableNameParser = new TableNameParser(mpBoundSql.sql());
//        ArrayList<TableNameParser.SqlToken> list = new ArrayList<>();
//        tableNameParser.accept(list::add);
//        System.out.println(mpBoundSql);
    }

    @Override
    public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
        InnerInterceptor.super.beforePrepare(sh, connection, transactionTimeout);
    }
}
